package com.wtwd.campus.controller;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.DeviceWhiteCall;
import com.wtwd.campus.entity.DeviceWhiteList;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.service.WhiteListService;
import com.wtwd.campus.utils.GetCurrentSysUserInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2021/06/29
 * @description
 */
@RestController
@RequestMapping("/whiteList")
public class WhiteListController {

    private static final Logger logger = LoggerFactory.getLogger(WhiteListController.class);
    @Autowired
    private WhiteListService whiteListService;
    @Autowired
    private GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;


    /**
     * 根据设备获取白名单列表
     * type:0 白名单  type:1情亲号
     * @param deviceId
     * @return
     */
    @GetMapping("/getWhiteList/{deviceId}/{type}")
    public RespEntity getWhiteList(@PathVariable Integer deviceId,@PathVariable Integer type) {
        logger.info("getWhiteList : param " + deviceId,type);
        List<DeviceWhiteList> deviceWhiteLists = whiteListService.getWhiteListByDeviceId(deviceId,type);
        return new RespEntity(RespCode.SUCCESS, deviceWhiteLists);
    }

    /**
     * 删除白名单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteDeviceWhiteList/{id}")
    public RespEntity deleteDeviceWhiteList(@PathVariable("id") Integer id) {
        logger.info("getWhiteList : param " + id);
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        return whiteListService.deleteDeviceWhiteList(id, sysUser);
    }

    /**
     * 添加白名单
     *
     * Map(map中含有两组数据,deviceId,imei和白名单列表集合)
     * @param deviceWhiteMap
     * @return
     */

    @PostMapping("/addDeviceWhiteList")
    public RespEntity addDeviceWhiteList(@RequestBody Map<String,Object> deviceWhiteMap) {
        logger.info("addDeviceWhiteList : param " + deviceWhiteMap);
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        return whiteListService.addDeviceWhiteList(deviceWhiteMap, sysUser);
    }

}
